/*
 * component: watchlist context menu
 * reference JS: permission.js
 * author: kidy lv
 */
var watchlistContextMenu; //
var watchlistContextmenuAbstract={
	getSRSubMenuData:function(sType){
        var res = [{
            txt: "Research",
            val: "9"
        }, QS_SMC.checkSecurityModule(sType, 3) ? {
            txt: "Quicktake",
            val: "3",
            showTab: true
        } : null];
		if (typeof QS_QT_TabMapping[sType] != 'undefined') {
			if(this.ticker&&$.trim(this.ticker)!=""&&gData[gIdMap[this.ticker]]["st816"]=="02") sType="PR";
			for (var id in QS_QT_TabMapping[sType]) {
				if (QS_SMC.checkSecurityModule(sType, parseFloat(id, 10))) {
					res.push({
						txt: QS_QT_TabMapping[sType][id].txt,
						qtTab: id
					});
				}
			}
		}
        return res;
    },
	getRightMenuData:function(sType, isEditDialog){
        return [
			sType=="XI"&&QSPC.checkPermission(0.07)?{
                txt: "Constituents",
                val: "cons"
            }:null,QS_SMC.checkSecurityModule(sType, 1) ? {
            txt: "Chart",
            val: "1",
            type: "",
            selectable: false,
            subMenu: [{
                txt: "Mountain",
                val: "mountain"
            }, {
                txt: "Line",
                val: "line"
            }, {
                txt: "Dot",
                val: "dot"
            }, {
                txt: "Above/Below",
                val: "ab"
            }, QS_SMC.checkSecurityModule(sType, 1.1) ? {
                txt: "OHLC",
                val: "ohlc"
            } : null, QS_SMC.checkSecurityModule(sType, 1.2) ? {
                txt: "HLC",
                val: "hlc"
            } : null, QS_SMC.checkSecurityModule(sType, 1.3) ? {
                txt: "Candlestick",
                val: "candle"
            } : null, {
                txt: "% Growth",
                val: "growth"
            }, {
                txt: "10K Growth",
                val: "growth10"
            }, QS_SMC.checkSecurityModule(sType, 1.4) ? {
                txt: "Dividend Effect",
                val: "dEffect"
            } : null]
        } : null, QS_SMC.checkSecurityModule(sType, 2) ? {
            txt: "News",
            val: "2"
        } : null, QS_SMC.checkSecurityModule(sType, 6) ? {
            txt: "Market Depth",
            val: "6"
        } : null, QS_SMC.checkSecurityModule(sType, 7) ? {
            txt: "Time & Sales",
            val: "7"
        } : null, QS_SMC.checkSecurityModule(sType, 8) ? {
            txt: "Options",
            val: "8"
        } : null, QS_SMC.checkSecurityModule(sType, 9) ? {
            txt: "Research",
            val: "9",
            selectable: false,
            subMenu: this.getSRSubMenuData(sType)
        } : null, QS_SMC.checkSecurityModule(sType, 10) ? {
            txt: "Quote",
            val: "10"
        } : null,{
			type: 'separator'
		},(sType=='FX'||(QSPC.checkPermission(4) && isEditDialog)) ? null : {
            txt: "Alert",
            val: "4"
        }, (isEditDialog? null : {
            txt: 'Sort',
            val: 'sort',
            subMenu: []
        }), this.config.remove?{
            txt: "Delete Row",
            val: "delete"
        }:null];
    }
}
watchlistContextMenu=function(config,callback){
	this.config={
		display: false,
        isTraceMouse: true,
        autoHide: true,
        fixedDirection: false,
		remove:true
	};
	this.callback=callback;
	this.objCache=null;
	this.chartStartDate=null;
	this.ticker=null;
	this.securityType=null;
	this.needSort=true;
	this.timer_contextmenu=null;
	this.chartStartDate=null;
	$.extend(this.config,config)
	var self=this;
	this.contextmenu = new Util.CascadeMenu($('body'),this.config,this.getRightMenuData('ST',true), {
		onClick: function(item){
			if(item==null) return;
	        if(typeof self.callback.onclick=='function'){
				self.callback.onclick(item,self.objCache);
			}
			self.contextmenu.hide();
		}
	});
};
watchlistContextMenu.prototype=$.extend({},watchlistContextmenuAbstract,{
	exch: function(sDate){
        var aDate = sDate.split("-");
        return aDate[2] + "-" + aDate[0] + "-" + aDate[1];
    },
	hide:function(){
		this.contextmenu.hide();
	},
	reBindSecondLevelMenu:function(value,subMenuData,sq){
		this.contextmenu.reBindSecondLevelMenu(value,subMenuData, sq);
	},
	setChartStartDate:function(date){
		this.chartStartDate=this.exch(date);
	},
	getChartStartDate:function(){
		return typeof this.chartStartDate == 'undefined'?null:this.chartStartDate;
	},
	securityType:function(type){
		this.securityType=type;
	},
	setTicker:function(ticker){
		this.ticker=ticker;
	},
	getIsEditDialog:function(){
		return this.isEditDialog;
	},
	setCallback:function(callback){
		this.callback=callback;
	},
	openContextMenu:function(e,obj,isEditDialog){//obj:{wsid,hid,ticker,type}
		this.isEditDialog=typeof isEditDialog=='undefined'?false:isEditDialog;
		this.ticker=obj.ticker;
		this.securityType = obj.securityType||"ST";
        this.contextmenu.reBind(this.getRightMenuData(this.securityType,this.isEditDialog));
		this.objCache=obj;
		if (!this.isEditDialog) {
			if(typeof this.callback.openContextMenuBefore=='function'){
	            this.callback.openContextMenuBefore(this.objCache);
	        }
		}
		this.contextmenu.show(e);
	}
});
var QS_watchlistContextMenu=null;